package com.edeweiss.community.dao.daoImp;


import com.edeweiss.community.dao.NoticeDao;
import com.edeweiss.community.domain.Notice;
import com.edeweiss.community.web.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class NoticeDaoImp implements NoticeDao {

	QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());

	@Override
	public List<Notice> findNotice() {
		String sql = "select *from notice";
		try {
			List<Notice> query = qr.query(sql,new BeanListHandler<Notice>(Notice.class));
			return query;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void delNotice(String nid){
		String sql = "delete from notice where nid=?";
		try{
			qr.update(sql,Integer.parseInt(nid));
		}catch(SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public int addNotice(Notice notice) {
		String sql = "insert into notice (name,times,content) value (?,?,?)";
		Object[] params = {notice.getName(),new Date(),notice.getContent()};
		try{
			return qr.update(sql,params);
		}catch(SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

}
